﻿using System;
using System.Management;

namespace ClusterAwareProperties
{
    /// <summary>
    /// Convert a Resourcetype ManagementObject to Resourcetype Class
    /// </summary>
    public static class ResourceTypeTranslate
    {
        /// <summary>
        /// Convert Resourcetype ManagementObject to Resourcetype Class
        /// </summary>
        /// <param name="clsres">Resourcetype class</param>
        /// <param name="queryObj">ManagementObject Resourcetype</param>
        /// <returns>Resourcetype class with properties converted</returns>
        public static ResourceTypeProperty Convert(this ResourceTypeProperty clsres, ManagementObject queryObj)
        {
            clsres.AdminExtensions = queryObj.PropertyValue<string[]>("AdminExtensions");
            clsres.Caption = queryObj.PropertyValue<string>("Caption");
            clsres.Characteristics = queryObj.PropertyEnum<UInt32, CharacteristisResourceType>("Characteristics", CharacteristisResourceType.Unknown);
            clsres.DeadlockTimeout = queryObj.PropertyValue<UInt32?>("DeadlockTimeout");
            clsres.DeleteRequiresAllNodes = queryObj.PropertyValue<bool>("DeleteRequiresAllNodes");
            clsres.Description = queryObj.PropertyValue<string>("Description");
            clsres.DisplayName = queryObj.PropertyValue<string>("DisplayName");
            clsres.DllName = queryObj.PropertyValue<string>("DllName");
            clsres.Flags = queryObj.PropertyValue<UInt32>("Flags");
            clsres.InstallDate = queryObj.PropertyValue<DateTime?>("InstallDate");
            clsres.IsAlivePollInterval = queryObj.PropertyValue<UInt32>("IsAlivePollInterval");
            clsres.LocalQuorumCapable = queryObj.PropertyValue<bool?>("LocalQuorumCapable");
            clsres.LooksAlivePollInterval = queryObj.PropertyValue<UInt32>("LooksAlivePollInterval");
            clsres.Name = queryObj.PropertyValue<string>("Name");
            clsres.PendingTimeout = queryObj.PropertyValue<UInt32?>("PendingTimeout");
            clsres.PrivateProperties = new PrivatePropertiesData(queryObj);
            clsres.QuorumCapable = queryObj.PropertyValue<bool>("QuorumCapable");
            clsres.Status = queryObj.PropertyEnum<string, StatusResource>("Status", StatusResource.Unknown);
            return clsres;
        }

        ///<summary>
        /// Convert Resourcetype class to ManagementObject and Save on Cluster
        ///</summary>
        ///<param name="clsres">Resourcetype class to converter</param>
        ///<param name="opSystem">Operation System where cluster is running</param>
        public static void SaveManagementObject(this ResourceTypeProperty clsres, WindowsOperationSystem opSystem)
        {
            clsres.Instance["AdminExtensions"] = clsres.AdminExtensions;
            clsres.Instance["Characteristics"] = (UInt32)clsres.Characteristics;
            if (opSystem == WindowsOperationSystem.Windows2008 || opSystem == WindowsOperationSystem.Windows2008R2)
            {
                clsres.Instance["DeadlockTimeout"] = clsres.DeadlockTimeout;
            }
           clsres.Instance["Description"] = clsres.Description;
            clsres.Instance["DisplayName"] = clsres.DisplayName;
            clsres.Instance["Flags"] = clsres.Flags;
            clsres.Instance["IsAlivePollInterval"] = clsres.IsAlivePollInterval;
            if (opSystem == WindowsOperationSystem.Windows2003 || opSystem == WindowsOperationSystem.Windows2003R2)
            {
                clsres.Instance["LocalQuorumCapable"] = clsres.LocalQuorumCapable;
            }
            clsres.Instance["LooksAlivePollInterval"] = clsres.LooksAlivePollInterval;
            if (opSystem == WindowsOperationSystem.Windows2008 || opSystem == WindowsOperationSystem.Windows2008R2)
            {
                clsres.Instance["PendingTimeout"] = clsres.PendingTimeout;
            }
            if (opSystem == WindowsOperationSystem.Windows2008 || opSystem == WindowsOperationSystem.Windows2008R2)
            {
                clsres.Instance["PrivateProperties"] = clsres.PrivateProperties.ConvertToManagemenBasetObject();
            }
            clsres.Instance["QuorumCapable"] = clsres.QuorumCapable;
            clsres.Instance.Put();
        }
    }
}
